var roleIdArr = JSON.parse(sessionStorage.getItem("roleIdArr"));
var roleNameArr = JSON.parse(sessionStorage.getItem("roleNameArr"));
//获取光标
$("#rolename").focus(function () {
    $("#tuan1").show()
});
$(document).bind("click", function (e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if ($(e.target).closest("#tuan1").length == 0 && $(e.target).closest("#rolename").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $("#tuan1").hide()
    }
})
/*查询所拥有角色*/
$(function () {
    var user_editData = JSON.parse(window.session_storage.getItem('user_editData'));
    if(user_editData.USER_ISEIDT == true){
        var roleIdArr = JSON.parse(sessionStorage.getItem("roleIdArr"));
        var roleNameArr = JSON.parse(sessionStorage.getItem("roleNameArr"));
        if(roleIdArr.length == 0||roleIdArr === undefined){
            var val = '';
        }else {
            for (var i = 0; i < roleIdArr.length; i++) {
                if( roleIdArr[i]==2) {
                    $("#codeWrap").append('<span  id="removeUEl' + roleIdArr[i] + '" class="btn radius size-M mr-10">' + roleNameArr[i] + '</span>');
                }else {
                    $("#codeWrap").append('<span  id="removeUEl' + roleIdArr[i] + '" class="btn radius size-M mr-10">' + roleNameArr[i] + '<a href="##" class="close-roles" onclick="delUserId(this,\'' + roleIdArr[i] + '\',\'' + roleNameArr[i] + '\')"></a></span>');
                }
            }
        }
    }else{
        var roleIdArr = JSON.parse(sessionStorage.getItem("roleIdArr"));
        var roleNameArr = JSON.parse(sessionStorage.getItem("roleNameArr"));
        if(roleIdArr.length == 0||roleIdArr === undefined){
            var val = '';
        }else {
            for (var i = 0; i < roleIdArr.length; i++) {
                if( roleIdArr[i]==2) {
                    $("#codeWrap").append('<span  id="removeUEl' + roleIdArr[i] + '" class="btn radius size-M mr-10">' + roleNameArr[i] + '</span>');
                }else {
                    $("#codeWrap").append('<span  id="removeUEl' + roleIdArr[i] + '" class="btn radius size-M mr-10">' + roleNameArr[i] + '<a href="##" class="close-roles" onclick="delUserId(this,\'' + roleIdArr[i] + '\',\'' + roleNameArr[i] + '\')"></a></span>');
                }
            }
        }  }
});

//对角色进行下拉框查询
function myFunction() {
    var user_data = {
        ROLE_NAME: $("#rolename").val()
    }
    var succCallBack = function (data, status, response) {
        if (data.roleList != "") {
            var jsonObj = data.roleList;
            if (jsonObj == "") {
                $("#tuan1").html("<li>无数据</li> ");
            } else {
                var optionstring = "";
                for (var i = 0; i < jsonObj.length; i++) {
                    optionstring += "<li onclick='addCodeFun(this.innerHTML," + jsonObj[i].ROLE_ID + ")'>" + jsonObj[i].ROLE_NAME + "</li>";
                }
                $("#tuan1").html(optionstring);
            }
        } else {
            $("#tuan1").html("<li>无数据</li> ");
        }
    }
    web.doAjax(root.accessInter.access_role_listRole, 'post', user_data, succCallBack);
}
// 添加角色
function addCodeFun(name, id) {
    if(roleIdArr!==null){
        var isAdd = true;
        for (var i = 0; i < roleIdArr.length; i++) {
            if(roleIdArr[i] == id){
                isAdd = false;
            }
        }
    }
    if(isAdd == true){
        if(roleIdArr!==null){
            layer.msg('角色添加成功', {icon: 1, time: 1000});
            $("#codeWrap").append('<span  id="removeUEl' + id + '" class="btn radius size-M mr-10">' + name + '<a href="##" class="close-roles" onclick="delUserId(this,\'' + id + '\',\\' + name+ '\')"></a></span>');
            $("#tuan1").hide();
            roleIdArr.push(id);
            roleNameArr.push(name);
        }
    }else {
        layer.msg('角色已添加,不能重复添加', {icon: 1, time: 1000});
    }
}

// 删除数组某个元素
function removeThisArrel(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == val) {
            arr.splice(i, 1);
        }
    }
}

//删除角色
function delUserId(thisEl, roleId, roleName) {
    removeThisArrel(roleIdArr, roleId);
    removeThisArrel(roleNameArr, roleName);
    $("#" + thisEl.parentNode.id).remove();
}

function save() {
    var account_add = JSON.parse(window.session_storage.getItem('account_add'));
    sessionStorage.setItem("roleIdArr", JSON.stringify(roleIdArr));
    sessionStorage.setItem("roleNameArr", JSON.stringify(roleNameArr));
    sessionStorage.setItem("account_add", JSON.stringify(account_add));
    var index = parent.location.reload();
    parent.layer.close(index);
}
